package com.xtm.exercise.base;

import com.google.common.base.Stopwatch;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.springframework.util.StopWatch;

import java.util.concurrent.TimeUnit;

/***
 *@Author: 王磊
 *@CreateTime: 2024-02-06  19:56
 *@Description: TODO
 *@title: StopWatchDemo
 */
@Slf4j
public class StopWatchDemo {

    /**
     * 运行时间监控
     * @throws InterruptedException
     */
    @Test
    public void test01() throws InterruptedException {
        StopWatch sw = new StopWatch();
        sw.start("TaskOneName");
        Thread.sleep(1000);
        sw.stop();
        sw.start("TaskTwoName");
        Thread.sleep(2000);
        sw.stop();
        System.out.println("prettyPrint ： " + sw.prettyPrint());
        System.out.println("获取所有任务执行花费的时间（秒） ：" + sw.getTotalTimeSeconds() + " s");
        System.out.println("获取所有任务执行花费的时间（毫秒） :"+sw.getTotalTimeMillis()+" ms");

        System.out.println("=====================================");
        Stopwatch started = Stopwatch.createStarted();
        Thread.sleep(2000);
        long timeConsuming = started.elapsed(TimeUnit.SECONDS);
        log.info("用时:{}秒", timeConsuming);

    }
}
